package finalproject;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JPanel;

public class MapGUI extends JPanel implements MouseListener, KeyListener {
    //init x, y, and objects, etc

    ArrayList<Unit> p1Units = new ArrayList();
    ArrayList<Unit> p2Units = new ArrayList();
    int x;
    int y;
    Headquarters defaultHeadquarters = new Headquarters();
    Cavalry defaultCavalry = new Cavalry();
    Pikeman defaultPikeman = new Pikeman();
    Swordsman defaultSwordsman = new Swordsman();
    int movementRemaining;
    int length = 7;
    int width = 3;
    int blockSize = 128;
    Unit currentUnit;
    boolean isMoving;
    Cell cells[][] = new Cell[length][width];
    Cell tempCell[][] = new Cell[length][width];
    Unit victoryCatch = new Unit();
    int vCatch;
    boolean victory = false;

    MapGUI() {
        //generates cells @ start
        for (int a = 0; a < length; a++) {
            for (int b = 0; b < width; b++) {
                cells[a][b] = new Cell(a * blockSize, b * blockSize, blockSize);
            }
        }
        //generates units @ start
        unitGen();
        this.addMouseListener(this);
        this.addKeyListener(this);

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        //draws cells
        for (int a = 0; a < length; a++) {
            for (int b = 0; b < width; b++) {
                g.drawRect(cells[a][b].getxCoord(), cells[a][b].getyCoord(), cells[a][b].getCellSize(), cells[a][b].getCellSize());
            }
        }
        //draws units
        for (int a = 0; a < length; a++) {
            for (int b = 0; b < width; b++) {
                if (cells[a][b].getTempUnit().getClass() == defaultHeadquarters.getClass()) {
                    g.setColor(Color.BLACK);
                    g.fillRect(a * blockSize, b * blockSize, blockSize, blockSize);
                } else if (cells[a][b].getTempUnit().getClass() == defaultCavalry.getClass()) {
                    g.setColor(Color.BLUE);
                    g.fillRect(a * blockSize, b * blockSize, blockSize, blockSize);
                } else if (cells[a][b].getTempUnit().getClass() == defaultPikeman.getClass()) {
                    g.setColor(Color.RED);
                    g.fillRect(a * blockSize, b * blockSize, blockSize, blockSize);
                } else if (cells[a][b].getTempUnit().getClass() == defaultSwordsman.getClass()) {
                    g.setColor(Color.GREEN);
                    g.fillRect(a * blockSize, b * blockSize, blockSize, blockSize);


                } else if (cells[a][b].getTempUnit().getClass() == Unit.class) {
                    g.setColor(Color.WHITE);

                    g.fillRect(a
                            * blockSize, b * blockSize, blockSize, blockSize);

                }
            }
        }
    }

    @Override
    //when clicked: select unit, get movement value
    public void mouseClicked(MouseEvent me) {
        int a = me.getX();
        int b = me.getY();
        x = a / blockSize;
        y = b / blockSize;
//        if (cells[x][y].tempUnit.getClass() == defaultCavalry.getClass()) {
//            Cavalry tempCavalry = new Cavalry();
//            cells[x][y].tempUnit = tempCavalry; //Unit in square selected.
//            movementRemaining = tempCavalry.getMovement();
//        } else if (cells[x][y].tempUnit.getClass() == defaultPikeman.getClass()) {
//            Pikeman tempPikeman = new Pikeman();
//            cells[x][y].tempUnit = tempPikeman; //Unit in square selected.
//            movementRemaining = tempPikeman.getMovement();
//        } else if (cells[x][y].tempUnit.getClass() == defaultSwordsman.getClass()) {
//            Swordsman tempSwordsman = new Swordsman();
//            cells[x][y].tempUnit = tempSwordsman; //Unit in square selected.
//            movementRemaining = tempSwordsman.getMovement();
//        }


        if (cells[x][y].tempUnit.getClass() == Pikeman.class
                || cells[x][y].tempUnit.getClass() == Swordsman.class
                || cells[x][y].tempUnit.getClass() == Cavalry.class) {
            currentUnit = cells[x][y].getTempUnit();
            isMoving = true;
            if (movementRemaining
                    <= 0) {
                movementRemaining = currentUnit.getFinalMovement();
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void keyTyped(KeyEvent ke) {
    }
    // actually move each sqaure, fight if its occupied

    @Override
    public void keyPressed(KeyEvent ke) {

        if (currentUnit != null) {
            if (isMoving) {
                if (movementRemaining > 0) {
                    if (ke.getKeyCode() == KeyEvent.VK_A) {
                        if (cells[x - 1][y].tempUnit.getClass() == defaultCavalry.getClass() || cells[x - 1][y].tempUnit.getClass() == defaultSwordsman.getClass() || cells[x - 1][y].tempUnit.getClass() == defaultPikeman.getClass() || cells[x - 1][y].tempUnit.getClass() == defaultHeadquarters.getClass()) {
                            fight(cells[x][y].tempUnit, cells[x - 1][y].tempUnit);
                        } else {
                            cells[x - 1][y].setTempUnit(cells[x][y].getTempUnit());
                            cells[x][y].setTempUnit(new Unit());
                            repaint();
                            revalidate();
                            movementRemaining = movementRemaining - 1;
                            x = x - 1;
                            y = y;
                            cells[x][y].tempUnit.setX(x);
                            cells[x][y].tempUnit.setY(y);
                        }
                    } else if (ke.getKeyCode() == KeyEvent.VK_D) {
                        if (cells[x + 1][y].tempUnit.getClass() == defaultCavalry.getClass() || cells[x + 1][y].tempUnit.getClass() == defaultSwordsman.getClass() || cells[x + 1][y].tempUnit.getClass() == defaultPikeman.getClass() || cells[x + 1][y].tempUnit.getClass() == defaultHeadquarters.getClass()) {
                            fight(cells[x][y].tempUnit, cells[x + 1][y].tempUnit);
                        } else {
                            cells[x + 1][y].setTempUnit(cells[x][y].getTempUnit());
                            cells[x][y].setTempUnit(new Unit());
                            repaint();
                            revalidate();
                            movementRemaining = movementRemaining - 1;
                            x = x + 1;
                            y = y;
                            cells[x][y].tempUnit.setX(x);
                            cells[x][y].tempUnit.setY(y);
                        }
                    } else if (ke.getKeyCode() == KeyEvent.VK_W) {
                        if (cells[x][y - 1].tempUnit.getClass() == defaultCavalry.getClass() || cells[x][y - 1].tempUnit.getClass() == defaultSwordsman.getClass() || cells[x][y - 1].tempUnit.getClass() == defaultPikeman.getClass() || cells[x][y - 1].tempUnit.getClass() == defaultHeadquarters.getClass()) {
                            fight(cells[x][y].tempUnit, cells[x][y - 1].tempUnit);
                        } else {
                            cells[x][y - 1].setTempUnit(cells[x][y].getTempUnit());
                            cells[x][y].setTempUnit(new Unit());
                            repaint();
                            revalidate();
                            movementRemaining = movementRemaining - 1;
                            x = x;
                            y = y - 1;
                            cells[x][y].tempUnit.setX(x);
                            cells[x][y].tempUnit.setY(y);
                        }
                    } else if (ke.getKeyCode() == KeyEvent.VK_S) {
                        if (cells[x][y + 1].tempUnit.getClass() == defaultCavalry.getClass() || cells[x][y + 1].tempUnit.getClass() == defaultSwordsman.getClass() || cells[x][y + 1].tempUnit.getClass() == defaultPikeman.getClass() || cells[x][y + 1].tempUnit.getClass() == defaultHeadquarters.getClass()) {
                            fight(cells[x][y].tempUnit, cells[x][y + 1].tempUnit);
                        } else {
                            cells[x][y + 1].setTempUnit(cells[x][y].getTempUnit());
                            cells[x][y].setTempUnit(new Unit());
                            repaint();
                            revalidate();
                            movementRemaining = movementRemaining - 1;
                            x = x;
                            y = y + 1;
                            cells[x][y].tempUnit.setX(x);
                            cells[x][y].tempUnit.setY(y);
                        }
                    }
                }
            }
        }
    }
    //fight method
    //kills (sets 'null' if lost)

    private void fight(Unit a, Unit b) {
        b.setHP(b.getHP() - a.getAttack());
        if (b.getHP() <= 0) {
            cells[b.getX()][b.getY()].tempUnit = new Unit();
            repaint();
            revalidate();
            victoryCheck();
        } else {
            a.setHP(a.getHP() - b.getAttack());
            if (a.getHP() - b.getAttack() <= 0) {
                cells[a.getX()][a.getY()].tempUnit = new Unit();
                repaint();
                revalidate();
                victoryCheck();
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent ke) {
    }
    //generates units at the start of level
    //also adds playerunits to arraylist

    public final void unitGen() {
        Headquarters p1Headquarters = new Headquarters();
        cells[0][0].setTempUnit(p1Headquarters);
        p1Headquarters.setHP(50);
        p1Headquarters.setAttack(0);
        p1Headquarters.setX(0);
        p1Headquarters.setY(0);
        Headquarters p2Headquarters = new Headquarters();
        cells[6][2].setTempUnit(p2Headquarters);
        p2Headquarters.setHP(50);
        p2Headquarters.setAttack(0);
        p2Headquarters.setX(6);
        p2Headquarters.setY(2);
        Pikeman p1Pikeman = new Pikeman();
        cells[1][0].setTempUnit(p1Pikeman);
        p1Pikeman.setHP(20);
        p1Pikeman.setAttack(4);
        p1Pikeman.setX(1);
        p1Pikeman.setY(0);
        Pikeman p2Pikeman = new Pikeman();
        cells[5][2].setTempUnit(p2Pikeman);
        p2Pikeman.setHP(20);
        p2Pikeman.setAttack(4);
        p2Pikeman.setX(5);
        p2Pikeman.setY(2);
        Cavalry p1Cavalry = new Cavalry();
        cells[0][1].setTempUnit(p1Cavalry);
        p1Cavalry.setHP(12);
        p1Cavalry.setAttack(7);
        p1Cavalry.setX(0);
        p1Cavalry.setY(1);
        Cavalry p2Cavalry = new Cavalry();
        cells[6][1].setTempUnit(p2Cavalry);
        p2Cavalry.setHP(12);
        p2Cavalry.setAttack(7);
        p2Cavalry.setX(6);
        p2Cavalry.setY(1);
        Swordsman p1Swordsman = new Swordsman();
        cells[0][2].setTempUnit(p1Swordsman);
        p1Swordsman.setHP(16);
        p1Swordsman.setAttack(4);
        p1Swordsman.setX(0);
        p1Swordsman.setY(2);
        Swordsman p2Swordsman = new Swordsman();
        cells[6][0].setTempUnit(p2Swordsman);
        p2Swordsman.setHP(16);
        p2Swordsman.setAttack(4);
        p2Swordsman.setX(6);
        p2Swordsman.setY(0);
        p1Units.add(p1Headquarters);
        p1Units.add(p1Pikeman);
        p1Units.add(p1Cavalry);
        p1Units.add(p1Swordsman);
        p2Units.add(p2Headquarters);
        p2Units.add(p2Pikeman);
        p2Units.add(p2Cavalry);
        p2Units.add(p2Swordsman);
    }

    public void victoryCheck() {
        int p1Alive = 3;
        int p2Alive = 3;
        for (int i = 0; i < p2Units.size(); i++) {
            if (p2Units.get(i).getClass() != Pikeman.class || p2Units.get(i).getClass() != Cavalry.class || p2Units.get(i).getClass() != Swordsman.class || p2Units.get(i).getClass() != Headquarters.class) {
                p2Alive--;
            }
        }
        for (int i = 0; i < p1Units.size(); i++) {
            if (p1Units.get(i).getClass() != Pikeman.class || p1Units.get(i).getClass() != Cavalry.class || p1Units.get(i).getClass() != Swordsman.class || p1Units.get(i).getClass() != Headquarters.class) {
                p2Alive--;
            }
        }
        if (p1Alive == 0) {
            System.out.println("Player 2 Wins!");
            victory = true;
        }
        if (p2Alive == 0) {
            System.out.println("Player 1 Wins!");
            victory = true;
        }
    }
}
